package com.t.bricks.bussiness.db.dao.customer.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.customer.CustomerDao;
import com.t.bricks.bussiness.db.entity.customer.Customer;
import com.t.bricks.bussiness.db.entity.customer.CustomerAccount;
import com.t.bricks.bussiness.db.mapper.customer.CustomerMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class CustomerDaoImpl implements CustomerDao<Customer> {

	@Autowired
	private CustomerMapper customerMapper;
	
	/**
	 * 用户类型对应的组织机构全称
	 */
	@Value("${app.department.fullname:未设置app.department.fullname配置项}")
	private String sDepartmentFullName;

	/**
	 * 删除
	 * @param uId 记录编号
	 * @return MsgEmity
	 */
	@Override
	@Transactional
	public MsgEmity del(String uId) {
		try {
			int i = customerMapper.delCustom(uId);
			if (i != 1) {
				Log.error("删除顾客记录未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "删除顾客记录失败！", 7001);
			}
			
			i = customerMapper.delAccount(uId);
			if (i != 1) {
				Log.error("删除顾客账户未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "删除顾客账户失败,请注意积分与余额必须都为0！", 7002);
			}
			
			i = customerMapper.delLoginUser(uId);
			if (i != 1) {
				Log.error("删除基本系统中的用户表未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "删除基本系统中的用户表失败！", 7003);
			}
			
			i = customerMapper.delUserAndDepartment(uId);
			if (i != 1) {
				Log.error("删除基本系统用户与部门关系表未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "删除基本系统用户与部门关系表失败！", 7004);
			}
		} catch (Exception e) {
			Log.error("删除时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return new MsgEmity(false, "删除失败！", 7005);
		}
		
		return new MsgEmity(true, "删除成功！", 7999);
	}

	/**
	 * 添加
	 * @Param customer
	 * @Param customerAccount
	 * @return MsgEmity
	 */
	@Override
	@Transactional
	public MsgEmity addCustom(Customer customer, CustomerAccount customerAccount) {
		try {
			int i = customerMapper.addLoginUser(customer);//添加顾客到基本系统表中的用户表
			if (i < 1) {
				Log.error("添加顾客到基本系统中的用户表未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "添加顾客到基本系统中的用户表失败！", 7001);
			}
			
			i = customerMapper.relUserAndDepartment(customer.getuId(), sDepartmentFullName);//添加顾客到基本系统表中的用户表
			if (i < 1) {
				Log.error("关联顾客与基本系统中的部门表操作未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "关联顾客与基本系统中的部门表操作失败！", 7002);
			}
			
			i = customerMapper.addCustom(customer);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "新增数据失败！", 7003);
			}
			
			i = customerMapper.addAccount(customerAccount);
			if (i < 1) {
				Log.error("新增关联支付账户信息未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "新增关联支付账户失败！", 7004);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return new MsgEmity(false, "新增失败！", 7005);
		}
		
		return new MsgEmity(true, "新增成功！", customer);
	}

	/**
	 * 修改密码
	 * @param uId
	 * @param sOldPass
	 * @param sNewPass
	 * @param sAesPass
	 * @param iVersion
	 * @param sModifieder
	 * @return
	 */
	@Override
	@Transactional
	public MsgEmity editPass(String uId, String sOldPass, String sNewPass,
			String sAesPass, int iVersion, String sModifieder) {
		try {
			int i = customerMapper.editPassToLoginUser(uId, sOldPass, sNewPass, sModifieder);
			if (i < 1) {
				Log.error("修改用户表未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "修改用户表失败,请检查用户表中的密码与顾客表密码是否一致!", 7001);
			}
			
			i = customerMapper.editPass(uId, sOldPass, sNewPass, sAesPass, iVersion, sModifieder);
			if (i < 1) {
				Log.error("修改未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "修改失败!", 7002);
			}
			
			i = customerMapper.addLog(uId, sOldPass, sNewPass, "登录密码", sModifieder);
			if (i < 1) {
				Log.error("添加日志未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "修改失败!", 7003);
			}
		} catch (Exception e) {
			Log.error("修改时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return new MsgEmity(false, "修改时发生异常!", 7004);
		}
		
		return new MsgEmity(true, "修改成功!", 7999);
	}

	/**
	 * 修改手势密码
	 * @param uId
	 * @param sOldPass
	 * @param sNewPass
	 * @param iVersion
	 * @param sModifieder
	 * @return
	 */
	@Override
	@Transactional
	public MsgEmity editSignPass(String uId, String sOldPass, String sNewPass,
			int iVersion, String sModifieder) {
		try {
			int i = customerMapper.editSignPass(uId, sOldPass, sNewPass, iVersion, sModifieder);
			if (i < 1) {
				Log.error("修改未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "修改失败!", 7001);
			}
			
			i = customerMapper.addLog(uId, sOldPass, sNewPass, "登录手势密码", sModifieder);
			if (i < 1) {
				Log.error("添加日志未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "修改失败!", 7002);
			}
		} catch (Exception e) {
			Log.error("修改时发生异常:", e.getMessage());
			return new MsgEmity(false, "修改时发生异常!", 7003);
		}
		
		return new MsgEmity(true, "修改成功!", 7999);
	}
	
	/**
	 * 根据手机号(sNo)与微信关注号查询AES密码
	 * @param sNo 手机号
	 * @param sWeChatOpenId 微信关注号
	 * @return
	 */
	@Override
	public MsgEmity findAesPass(String sNo, String sWeChatOpenId) {
		try {
			String sAesPass = customerMapper.findAesPass(sNo, sWeChatOpenId);
			if (null == sAesPass) {
				return new MsgEmity(false, "未找到相关数据", 7001);
			}
			
			if ("".equals(sAesPass.trim()) || "-1".equals(sAesPass)) {
				return new MsgEmity(false, "AES密码未设定", 7002);
			}
			
			return new MsgEmity(true, "获取成功", sAesPass);
		} catch (Exception e) {
			Log.error("根据手机号(sNo)与微信关注号查询AES密码时发生异常:", e.getMessage());
			return new MsgEmity(false, "根据手机号(sNo)与微信关注号查询AES密码发生异常!", 7003);
		}
	}
	
	/**
	 * 根据手机号(sNo)判断是否与关注号进行绑定
	 * @param sNo 手机号
	 * @return
	 */
	@Override
	public MsgEmity hasOpenId(String sNo) {
		try {
			String sAesPass = customerMapper.hasOpenId(sNo);
			if (null == sAesPass) {
				return new MsgEmity(false, "未找到相关数据", 7001);
			}
			
			return new MsgEmity(true, "查询成功", sAesPass);
		} catch (Exception e) {
			Log.error("根据手机号(sNo)判断是否与关注号进行绑定 时发生异常:", e.getMessage());
			return new MsgEmity(false, "根据手机号(sNo)判断是否与关注号进行绑定 发生异常!", 7002);
		}
	}

	/**
	 * 绑定关注号
	 * @param uId 记录编号
	 * @param sOpenId 关注号
	 * @param sPass 当前密码
	 * @param iType 类型(1:微信)
	 * @return
	 */
	@Override
	public MsgEmity bindingOpenId(String uId, String sOpenId, Integer iType, String sAesPass) {
		try {
			int i = customerMapper.bindingOpenId(uId, sOpenId, iType, sAesPass);
			if (i < 1) {
				Log.error("绑定操作未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "绑定操作失败!", 7001);
			}
		} catch (Exception e) {
			Log.error("绑定操作发生异常:", e.getMessage());
			return new MsgEmity(false, "绑定操作发生异常!", 7002);
		}
		
		return new MsgEmity(true, "绑定操作成功!", 7999);
	}
	
	/**
	 * 设置微信用户统一标识
	 * @param uId 用户编号
	 * @param sWechatUid 微信用户统一标识
	 * @return
	 */
	@Override
	public MsgEmity setWechatid(String uId, String sWechatUid) {
		try {
			int i = customerMapper.setWechatid(uId, sWechatUid);
			if (i < 1) {
				Log.error("设置微信用户统一标识未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "设置微信用户统一标识操作失败!", 7001);
			}
		} catch (Exception e) {
			Log.error("设置微信用户统一标识操作发生异常:", e.getMessage());
			return new MsgEmity(false, "设置微信用户统一标识操作发生异常!", 7002);
		}
		
		return new MsgEmity(true, "设置微信用户统一标识操作成功!", 7999);
	}
	
	/**
	 * 根据微信用户统一标识获取电话信息
	 * @param sWechatUid 微信用户统一标识
	 * @return
	 */
	@Override
	public MsgEmity getNoByWechatUid(String sWechatUid) {
		try {
			Map<String, Object> map = customerMapper.getNoByWechatUid(sWechatUid);
			if (null == map) {
				return new MsgEmity(false, "未找到相关数据", 7001);
			}
			
			return new MsgEmity(true, "获取成功", map);
		} catch (Exception e) {
			Log.error("根据微信用户统一标识获取电话信息时发生异常:", e.getMessage());
			return new MsgEmity(false, "根据微信用户统一标识获取电话信息发生异常!", 7002);
		}
	}
	
	/**
	 * 判断微信用户唯一标识是否已经绑定
	 * @param sWechatUid 微信用户统一标识
	 * @return 返回手机号
	 */
	@Override
	public MsgEmity hasWechatUid(String sWechatUid) {
		try {
			String sNo = customerMapper.hasWechatUid(sWechatUid);
			if (null == sNo || "".equals(sNo.trim()) || "-1".equals(sNo)) {
				return new MsgEmity(false, "未找到相关数据", 7001);
			}
			
			return new MsgEmity(true, "查询成功", sNo);
		} catch (Exception e) {
			Log.error("判断微信用户唯一标识是否已经绑定 时发生异常:", e.getMessage());
			return new MsgEmity(false, "判断微信用户唯一标识是否已经绑定发生异常!", 7002);
		}
	}
	
	/**
	 * 根据微信用户唯一标识查询AES密码
	 * @param sWechatUid 微信用户统一标识
	 * @return 返回AES密码
	 */
	@Override
	public MsgEmity findAesPassByWechatUid(String sWechatUid) {
		try {
			Map<String, Object> map = customerMapper.findAesPassByWechatUid(sWechatUid);
			if (null == map) {
				return new MsgEmity(false, "未找到相关数据", 7001);
			}
			
			Object sAesPass = map.get("sAesPass");
			if (null == sAesPass || "".equals(sAesPass.toString().trim()) || "-1".equals(sAesPass)) {
				return new MsgEmity(false, "AES密码未设定", 7002);
			}
			
			return new MsgEmity(true, "获取成功", map);
		} catch (Exception e) {
			Log.error("根据微信用户唯一标识查询AES密码时发生异常:", e.getMessage());
			return new MsgEmity(false, "根据微信用户唯一标识查询AES密码发生异常!", 7003);
		}
	}
	
}
